package com.chance.cc.crawler.development.command.job.domain.taobao;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.proxy.Proxy;
import com.chance.cc.crawler.core.filter.FilterInfo;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.queue.crawler.CrawlerQueueConfig;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.command.job.CrawlerJobCommand;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * created by CC on 2020/11/26
 * mail 279020185@qq.com
 */
public class TaobaoSearchCrawlerScheduleJob {

    public static String domainId = "taobao";
    private static String searchUrl = "https://s.taobao.com/search";

    private static String service_ip = "192.168.1.215";
    private static int service_port = 9599;

    private static Proxy proxy = new Proxy();
    static {
        //代理配置
        proxy.setHost("http-dyn.abuyun.com");
        proxy.setPort(9020);
        proxy.setUsername("HL89Q19E86E2987D");
        proxy.setPassword("71F33D94CE5F7BF2");
    }

    public static void main(String[] args) {
        publishTBSearchCrawlerScheduleJobInfo();
    }

    public static CrawlerJob publishTBSearchCrawlerScheduleJobInfo(){


        CrawlerJob taobaoSchduler = taobaoSchduler(domainId);

        //发布定时采集作业
        //发布定时采集作业
        CrawlerJobCommand crawlerJobCommand = new CrawlerJobCommand(service_ip,service_port);
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domainId);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(taobaoSchduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("淘宝关键词搜索定时采集");
        crawlerScheduleJob.setCrawlerKey(taobaoSchduler.generateCrawlerKey());
        HttpPage page = crawlerJobCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布作业：" + page.getRawText());
        return taobaoSchduler;

    }

    /**
     * taobao采集
     * @param domainId
     * @return
     */
    public static CrawlerJob taobaoSchduler(String domainId) {

        FilterInfo countFilterInfo = new FilterInfo();
        countFilterInfo.setFilter(CrawlerEnum.CrawlerRecordFilter.count);
        countFilterInfo.setCountTotalNum(100);
        countFilterInfo.setCurCount(0);

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domainId, turnPage)
                .httpUrl(searchUrl)
                .releaseTime(System.currentTimeMillis())
                .filter(CrawlerEnum.CrawlerRecordFilter.key)
                .addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-","filter",domainId,"search","queue")))
                .turnPageFilterInfo(countFilterInfo)
                .needWashed(false)
                .needParsed(false)
                .proxy(proxy)
                .build();
        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);

        String resultQueueName = "taobao_search_result";
        CrawlerQueueConfig crawlerQueueConfig = new CrawlerQueueConfig(
                resultQueueName,
                CrawlerQueueConfig.Content.result,
                CrawlerQueueConfig.Storage.redis,
                CrawlerQueueConfig.Structure.list);

        //关键词 source record
        CrawlerRequestRecord keywordCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("taobao_keywords", turnPageItem)
                .httpUrl("http://"+service_ip+":"+service_port+"/v1/meta/taobao/keys" )
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        //user oauth infos source record
        CrawlerRequestRecord userOauthInfoCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("taobao_user_oauth_infos", turnPageItem)
                .httpUrl("http://"+service_ip+":"+service_port+"/crawler/oauth/api/v1/taobao/userOauthInfos")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        //商品信息同步到mysql callback record
        CrawlerRequestRecord syncCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("taobao_sync", turnPageItem)
                .httpUrl("http://"+service_ip+":"+service_port+"/crawler/domain/common/api/v1/taobao/search/results/sync?resultQueue=" + resultQueueName)
                .requestLabelTag(supportCallback)
                .requestLabelTag(internalDownload)
                .build();

        return CrawlerJob.builder()
                .triggerInfo(
                        domainId,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-","search",CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-","crawler",domainId,"queue")))
                .queueResultPipeline(null, crawlerQueueConfig)//redis pipeline result
                .fileResultPipeline(null, "/data/chance_crawler_test/data/taobao/taobao_search.log", false)
//                .fileResultPipeline(null, "D:\\chance\\log\\taobao\\taobao_search.log", false)
                .requestRecord(requestRecord)
                .supportRecord(keywordCrawlerRecord)
                .supportRecord(userOauthInfoCrawlerRecord)
                .supportRecord(syncCrawlerRecord)
                .build();
    }

}
